<?php

namespace App\Http\Controllers;


use App\Models\Slideshow;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class SlideShowsController extends Controller
{

    public function postModifySort(Request $request)
    {
        $action = $request->input('action');
        $sort = $request->input('sort');

        if (!in_array($action, ['up', 'down']) || !$sort)
            return json_encode(['status' => 'error', 'msg' => '缺少参数']);

        $diff = $action == 'up' ? '<' : '>';
        $orderBy = $action == 'up' ? 'desc' : 'asc';
        $slideshows = Slideshow::where('sort', $diff, $sort)
            ->where('deleted_at', null)
            ->select('id', 'sort')
            ->orderBy('sort', $orderBy)
            ->first();
        if (!$slideshows)
            return json_encode(['status' => 'error', 'msg' => '已为顶级或者最低级']);

        Slideshow::where('sort', $sort)
            ->where('deleted_at', null)
            ->update(['sort' => $slideshows->sort]);

        $slideshows->sort = $sort;
        $slideshows->save();

        return json_encode(['status' => 'success', 'msg' => '']);
    }

}